layui.define(["form","laydate","button"],function(exports) {
	
	class SelectTree {
		
		constructor (opt){
            if($(opt.elem).length<1){return alert("查询控件容器不存在");}
            this.$el = $(opt.elem);
            this._init(opt);
       	}
		
		_init(opt){
			var $th = this;
			var $dom = this.$el;
			var id = $.createGuid();
			var html = `
				<div class="layui-form-select selectTree" id="select_${id}">
					<div class="layui-select-title">
						<input type="text" class="layui-input" style="display: none;" 
							name="${ $dom.attr('name') ||  $dom.attr('id-field') || '' }"
							data-opera="${ $dom.attr('data-opera') || '' }"
						/>
						<input type="text" placeholder="请选择" class="layui-input" readonly form-dataType="text">
						<i class="layui-edge"></i>
					</div>
				</div>
			`;
			$dom.replaceWith(html);
			$("body").append(`<div id='popup_${id}' class="xw-selectTree-popup"></div>`);
			var $select = $('#select_'+id);
			var $popup = $('#popup_'+id);
			var popHeight =  $popup.height();
			$select.click(function(e){
				e.stopPropagation();
				if( $(this).find('.layui-input[disabled]').length==0 ){
					$(this).addClass('layui-form-selected');
					var top = $(this).offset().top;
					var height = popHeight;
                    var maxHeight = window.innerHeight - (top+ $(this).height()) - 30;
                    if(maxHeight<height){   height  = maxHeight;  }
					$popup.css({
						"left": $(this).offset().left ,
						"top": top + $(this).height() + 1,
						'height': height
					}).show();
				}
			});
			$(document).on('click','body',function(e){
				e.stopPropagation();
				if($(e.target).is('.xw-selectTree-popup')){
					$select.addClass('layui-form-selected');
				}else{
					$popup.hide();
					$select.removeClass('layui-form-selected');
				}
			});
			var selectedNodeFn = opt.selectedNode;
            var loadSuccessFn = opt.loadSuccess;
			opt.elem = '#popup_'+id;
			opt.isList = false  ? false : true;
			opt.loadSuccess=function(data){                      //加载成功的回调
				if(opt.isDefaultSelected){
					if(!localStorage._selectTreeSelectedId){
						localStorage._selectTreeSelectedId = -1;
					}
					$th.tree.selectNode(opt.idField,localStorage._selectTreeSelectedId);
				}
				if(loadSuccessFn){ loadSuccessFn(data) }
            }
			opt.selectedNode=function(obj){
            	var text = obj.data[opt.textField] || '';
            	var treeId = obj.data[opt.idField] || '';
            	$select.find('.layui-input[type="text"]').eq(0).val(treeId);
            	$select.find('.layui-input[type="text"]').eq(1).val(text);
            	$popup.hide();
            	$select.removeClass('layui-form-selected');
            	if(opt.isDefaultSelected){
            		localStorage._selectTreeSelectedId = treeId;
            	}
            	if(selectedNodeFn){ selectedNodeFn(obj) }
			}
			this.tree = layui.tree.render(opt);
		}
	}
	exports('selectTree',{  render:function(opt){ return new SelectTree(opt);  } });
});

